# -*- coding:UTF-8 -*-
'''
开发环境：PyCharm
创建日期：2025/8/20 16:21
作   者：新启典学苑-樱桃
描   述：1. 手动设置cookie
        2. 自动管理cookie
           创建一个session对象，通过session对象自动管理Cookie。
'''
import requests


def test_手动设置Cookie():
    print("登录")
    url = "http://192.168.1.29:8000/api/user/login"
    cs = { "userName": "admin", "password": "123456", "remember": False }
    r = requests.post(url, json=cs)
    print("*" * 50)
    print(r.json())
    print("*" * 50)
    print("cookie：", r.cookies)
    print("查询学科")
    url = "http://192.168.1.29:8000/api/admin/education/subject/list"
    r = requests.post(url, cookies=r.cookies)
    print(r.json())

def test_自动管理Cookie():
    print("登录")
    s = requests.Session() # 创建一个session对象
    print("===========登录前的cookie:", s.cookies, "===========")
    url = "http://192.168.1.29:8000/api/user/login"
    cs = {"userName": "admin", "password": "123456", "remember": False}
    r = s.post(url, json=cs)  # 使用session发送请求，会自动管理headers、cookies等
    print(r.json())
    print("===========登录后的cookie:", s.cookies, "===========")
    print("查询学科")
    url = "http://192.168.1.29:8000/api/admin/education/subject/list"
    r = s.post(url)
    print(r.json())
    print("退出登录")
    url = "http://192.168.1.29:8000/api/user/logout"
    r = s.post(url)
    print(r.json())
    print("===========退出登录后的cookie:", s.cookies, "===========")




